package cn.liujianbo.example.springbean.bean1;

import cn.liujianbo.example.springbean.bean1.component.AppConfig;
import cn.liujianbo.example.springbean.bean1.component.PrototypeUserService;
import cn.liujianbo.example.springbean.bean1.component.UserService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @author 刘建波
 * @date 2021-10-10 10:43
 * 单例bean和原型bean
 */
public class Test {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
        // 1. spring 单例bean，根据名字或者类型获取到的为同一对象
        System.out.println(context.getBean("userService"));
        System.out.println(context.getBean(UserService.class));
        // 2. spring 原型bean，每次获取到的bean都不一样
        System.out.println(context.getBean("prototypeUserService"));
        System.out.println(context.getBean(PrototypeUserService.class));
        // 3. spring 单例bean，@Component  @Bean 手动生成多个bean，通过类型获取会报错，通过指定名称可以得到对应bean
        System.out.println(context.getBean("manyUserService"));
        // System.out.println(context.getBean(ManyUserService.class));
    }
}
